package com.panda.common.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.panda.common.constant.WebResultStatus;

/**
 * 	控制层基类
 */
public class BaseController {
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	protected String getString(HttpServletRequest request, String string) {
		return getString(request, string, "");
	}
	
	protected String getString(HttpServletRequest request, String string, String defaultStr) {
		String parameter = request.getParameter(string);
		if (parameter == null || "".equals(parameter)) {
			return defaultStr;
		} else {
			return parameter;
		}
	}
	
	protected Integer getInt(HttpServletRequest request, String string) {
		return getInt(request, string, 0);
	}
	
	protected Integer getInt(HttpServletRequest request, String string, Integer defaultInt) {
		String parameter = request.getParameter(string);
		if (parameter == null || "".equals(parameter)) {
			return defaultInt;
		} else {
			try {
				return Integer.valueOf(parameter);
			} catch (Exception e) {
				StringWriter sw = new StringWriter();e.printStackTrace(new PrintWriter(sw, true));logger.error(sw.toString());
				return defaultInt;
			}
		}
	}
	
	protected boolean getBoolean(HttpServletRequest request, String string, boolean b) {
		String parameter = request.getParameter(string);
		if (parameter == null) {
			return b;
		} else {
			return "true".equals(parameter) ? true : false;
		}
	}
	
	protected static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	
	/**
	 * 原生返回响应内容接口
	 * @param response
	 * @param writeContent
	 * @param contentType
	 */
	protected void ajaxResultText(HttpServletResponse response, String writeContent, String contentType) {
		if (contentType == null) {
			response.setContentType("text/html");
		} else {
			response.setContentType(contentType);
		}
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		try {
			PrintWriter out = response.getWriter();
			out.print(writeContent);
			out.flush();
			out.close();
		} catch (IOException e) {
			StringWriter sw = new StringWriter();e.printStackTrace(new PrintWriter(sw, true));logger.error(sw.toString());
		}
	}
	
	protected void ajaxResultText(HttpServletResponse response, String writeContent) {
		ajaxResultText(response, writeContent, "text/html;charset=UTF-8");
	}
	
	/**
	 *  获取数据列表时，返回值的格式
	 * @param code
	 * @param msg
	 * @param data
	 * @param count
	 * @return
	 * @throws Exception
	 */
	protected Map<String, Object> getResultMap(Object code, String msg, Collection<?> data, Number count) {
		  Map<String, Object> map = new HashMap<String, Object>();
		  map.put("code", code);
		  map.put("msg", msg);
		  map.put("data", data);
		  map.put("count", count);
		  return map;
	}
	
	/**
	 * 成功获取数据列表，返回值的格式
	 * @param msg
	 * @param data
	 * @param count
	 * @return
	 * @throws Exception
	 */
	protected Map<String, Object> getSuccessResultMap(String msg, Collection<?> data, Number count) {
		  return this.getResultMap(WebResultStatus.status_normal.getValue(), msg, data, count);
	}
	
	/**
	 *  获取数据失败，返回值的格式
	 * @param msg
	 * @param data
	 * @param count
	 * @return
	 * @throws Exception
	 */
	protected Map<String, Object> getErrorResultMap(String msg) {
		  return this.getResultMap(WebResultStatus.status_error.getValue(), msg, null, 0);
	}
	
	/**
	 * 成功获取数据对象，返回值的格式
	 * @param msg
	 * @param data
	 * @return
	 * @throws Exception
	 */
	protected Map<String, Object> getSuccessResultMap(String msg, Object data) {
		  Map<String, Object> map = new HashMap<String, Object>();
		  map.put("code", WebResultStatus.status_normal.getValue());
		  map.put("msg", msg);
		  map.put("data", data);
		  map.put("count", 1);
		  return map;
	}
	
}